package com.choosefine.it.project.controller;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.utils.StringUtils;
import com.choosefine.it.project.model.vo.ProjectInfoListVO;
import com.choosefine.it.project.model.vo.ProjectInfoWithTeamIdVO;
import com.choosefine.it.project.service.ProjectInfoListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 承建公司获得项目列表
 * 
 * @author 毛俊
 * @date 2016-12-29
 */
@RestController
@RequestMapping("/getProjectInfoList")
@Api(value = "承建公司获得项目列表", description = "毛俊")
public class ProjectInfoListController extends BaseController {
	@Autowired
	ProjectInfoListService projectInfoListService;

	@ApiImplicitParam(name = "userCode", value = "承建公司租户编号", required = true, dataType = "String", paramType = "path")
	@RequestMapping(value = "/getProjectInfoListByCompanyUserCode/{userCode}", method = RequestMethod.GET)
	@ApiOperation(value = "根据承建公司租户编号usercode查找", response = ProjectInfoListVO.class)
	public String getProjectInfoListByCompanyUserCode(@PathVariable String userCode) {
		String result = null;
		List<ProjectInfoListVO> list = null;
		if (StringUtils.isNotBlank(userCode)) {
			list = projectInfoListService.getProjectInfoListByCompanyUserCode(userCode);
			if (list.size() >0) {
				result = responseArraySuccess(list, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
			} else {
				result = responseFail(ResultCode.FAILED, ResultCode.MSG_DATA_ISEMPTY);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_ERROR);
		}
		return result;
	}

	@ApiImplicitParams({
			@ApiImplicitParam(name = "managerUserCode", value = "项目经理租户编号", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "companyUserCode", value = "承建公司租户编号", required = true, dataType = "String", paramType = "query") })
	@RequestMapping(value = "/getListByManagerAndCompanyUserCode", method = RequestMethod.GET)
	@ApiOperation(value = "根据项目经理和承建公司租户编号查找", response = ProjectInfoListVO.class)
	public String getListByManagerAndCompanyUserCode(
			@RequestParam(value = "managerUserCode", required = true) String managerUserCode,
			@RequestParam(value = "companyUserCode", required = true) String companyUserCode) {
		String result = null;

		if (managerUserCode ==null || "".equals(managerUserCode)) {
			return responseFail(ResultCode.MSG_PARAMS_ERROR);
		}
		if (companyUserCode ==null || "".equals(companyUserCode)) {
			return responseFail(ResultCode.MSG_PARAMS_ERROR);
		}
		Map<String, String> params = new HashMap<String, String>();
		params.put("companyUserCode", companyUserCode);
		params.put("managerUserCode", managerUserCode);
		List<ProjectInfoListVO> list = null;

		list = projectInfoListService.getListByManagerAndCompanyUserCode(params);
		if (list.size() > 0) {
			result = responseArraySuccess(list, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
		} else {
			result = responseFail(ResultCode.FAILED, ResultCode.MSG_DATA_ISEMPTY);
		}
		return result;
	}

	@ApiImplicitParams({
			@ApiImplicitParam(name = "teamUserCode", value = "分包承包人租户编号", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "companyUserCode", value = "承建公司租户编号", required = true, dataType = "String", paramType = "query") })
	@RequestMapping(value = "/getListByTeamAndCompanyUserCode", method = RequestMethod.GET)
	@ApiOperation(value = "根据分包承包人和承建公司租户编号查找", response = ProjectInfoWithTeamIdVO.class)
	public String getListByTeamAndCompanyUserCode(
			@RequestParam(value = "teamUserCode", required = true) String teamUserCode,
			@RequestParam(value = "companyUserCode", required = true) String companyUserCode) {
		String result = null;
		if (teamUserCode == null || "".equals(teamUserCode)) {
			return responseFail(ResultCode.MSG_PARAMS_ERROR);
		}
		if (companyUserCode == null || "".equals(companyUserCode)) {
			return responseFail(ResultCode.MSG_PARAMS_ERROR);
		}
		Map<String, String> params = new HashMap<String, String>();
		params.put("companyUserCode", companyUserCode);
		params.put("teamUserCode", teamUserCode);
		List<ProjectInfoWithTeamIdVO> list = null;

		list = projectInfoListService.getListByTeamAndCompanyUserCode(params);
		if (list.size() > 0) {
			result = responseArraySuccess(list, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
		} else {
			result = responseFail(ResultCode.FAILED, ResultCode.MSG_DATA_ISEMPTY);
		}
		return result;
	}
}
